package com.yjt.fresh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.activity.AtyAfterSaleDetail;
import com.yjt.fresh.activity.AtyToReturnGoods;
import com.yjt.fresh.adpater.AdpRvFrgAfterSaleCheck;
import com.yjt.fresh.api.ApiGetAfterSaleCheck;
import com.yjt.fresh.base.BaseSimpleFragment;
import com.yjt.fresh.bean.BeanAfterSaleCheck;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by UPC on 2018/8/16.
 */

public class FrgAfterSaleCheck extends BaseSimpleFragment {
    private static final int PAGE_SIZE = 20;
    private static final int REQUEST_CODE = 1008;
    @BindView(R.id.rvAppFrgAfterSaleCheck)
    RecyclerView mRv;
    @BindView(R.id.srlAppFrgAfterSalCheck)
    SwipeRefreshLayout mRefreshLayout;

    private int mNextRequestPage = 1;
    private AdpRvFrgAfterSaleCheck mAdpAfterSaleCheck;
    private boolean isLoadMore;

    @Override
    protected int getContentView() {
        return R.layout.app_frg_after_sale_check;
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    protected void initEvents() {
        initRv();
        getData();
        initRefresh();
        initClick();
    }

    private void initClick() {
        mAdpAfterSaleCheck.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tvAppRecycleItemFrgAfterSaleCheckToReturnGoods){
                    //去退货
                    Intent intent = new Intent(mContext, AtyToReturnGoods.class);
                    intent.putExtra(MyConstants.ORDER_ID,mAdpAfterSaleCheck.getItem(position).getBACKORDER_ID());
                    startActivityForResult(intent,REQUEST_CODE);
                }else if (view.getId() == R.id.tvAppRecycleItemFrgAfterSaleCheckDetail){
                    //查看详情
                    Intent intent = new Intent(mContext, AtyAfterSaleDetail.class);
                    intent.putExtra(MyConstants.ORDER_INFO,mAdpAfterSaleCheck.getItem(position));
                    startActivity(intent);

                }
            }
        });
    }

    private void initRefresh() {
        mAdpAfterSaleCheck.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    private void refresh() {
        mNextRequestPage = 1;
        isLoadMore = false;
        getData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser && mContext != null){
            refresh();
        }
    }

    private void getData() {
        ApiGetAfterSaleCheck apiGetAfterSaleCheck = new ApiGetAfterSaleCheck(new HttpOnNextListener<List<BeanAfterSaleCheck>>() {

            @Override
            public void onNext(List<BeanAfterSaleCheck> beanAfterSaleChecks) {
                mAdpAfterSaleCheck.setEmptyView(R.layout.view_empty);
                if (isLoadMore) {
                    setData(false, beanAfterSaleChecks);
                } else {
                    setData(true, beanAfterSaleChecks);
                    mRefreshLayout.setRefreshing(false);
                }
            }

            @Override
            public void onReLogin() {

            }
        }, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage);
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiGetAfterSaleCheck.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetAfterSaleCheck);

    }

    private void setData(boolean isRefresh, List<BeanAfterSaleCheck> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpAfterSaleCheck.setNewData(data);
        } else {
            if (size > 0) {
                mAdpAfterSaleCheck.addData(data);
                mAdpAfterSaleCheck.loadMoreComplete();
            }else {
                mAdpAfterSaleCheck.loadMoreEnd(true);
            }
        }

        if (size < 20){
            mAdpAfterSaleCheck.loadMoreEnd(true);
        }
    }

    private void initRv() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanAfterSaleCheck> list = new ArrayList<>();
        mAdpAfterSaleCheck = new AdpRvFrgAfterSaleCheck(R.layout.app_recycle_item_frg_after_sale_check, list);
        mRv.setAdapter(mAdpAfterSaleCheck);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        refresh();
    }
}
